package com.ita.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;

public class DBUtil {

	private static final String DRIVER_NAME;
	private static final String URL;
	private static final String USER_NAME;
	private static final String PASSWORD;
	
//	private static final String DRIVER_NAME = "DRIVER_NAME";
//	private static final String URL = "URL";
//	private static final String USER_NAME = "USER_NAME";
//	private static final String PASSWORD = "PASSWORD";
//	private static final Map<String, String> config = new HashMap<String, String>();
	
	private static DataSource jndiDs;
	private static BasicDataSource ds;

	private static Logger logger = Logger.getLogger(DBUtil.class);
	
	static {
		Properties properties = PropertiesUtil.getProp();
		DRIVER_NAME = properties.getProperty("DRIVER_NAME");
		URL = properties.getProperty("URL");
		USER_NAME = properties.getProperty("USER_NAME");
		PASSWORD = properties.getProperty("PASSWORD");
		
		/*Properties prop = new Properties();
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(
					"config/db.properties"));
			prop.load(in);

			config.put(DRIVER_NAME, prop.getProperty(DRIVER_NAME));
			config.put(URL, prop.getProperty(URL));
			config.put(USER_NAME, prop.getProperty(USER_NAME));
			config.put(PASSWORD, prop.getProperty(PASSWORD));

		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			prop = null;
			if (in != null) {
				try {
					in.close();
					in = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}*/
		
		ds = new BasicDataSource();
		
		ds.setUrl(URL);
		ds.setDriverClassName(DRIVER_NAME);
		ds.setUsername(USER_NAME);
		ds.setPassword(PASSWORD);
		
//		ds.setUrl(config.get(URL));
//		ds.setDriverClassName(config.get(DRIVER_NAME));
//		ds.setUsername(config.get(USER_NAME));
//		ds.setPassword(config.get(PASSWORD));
	
	}

	public static Connection createConnection() {

		Connection conn = null;
		try {
			Class.forName(DRIVER_NAME);
			conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
//			Class.forName(config.get(DRIVER_NAME));
//			conn = DriverManager.getConnection(config.get(URL),
//					config.get(USER_NAME), config.get(PASSWORD));
		} catch (ClassNotFoundException e) {
			logger.error(e.getMessage());
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
		
		return conn;
	}

	public static Connection createByDs() {

		Connection conn = null;
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
		return conn;
	}

	public static Connection createByJNDI() {

		Connection conn = null;
		
		try {
			Context cxt = new InitialContext();
			jndiDs = (DataSource) cxt.lookup("java:/comp/env/jdbc/myoracle");
			return jndiDs.getConnection();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return conn;
	}

	public static void close(Connection conn, PreparedStatement pst,
			ResultSet rSet) {

		try {
			if (conn != null) {
				conn.close();
			}
			if (pst != null) {
				pst.close();
			}
			if (rSet != null) {
				rSet.close();
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}

}
